//
//  TimeChartPoint.m
//  CultureTrade
//
//  Created by SZFT1 on 16/1/6.
//  Copyright (c) 2016年 cheng.zongxin. All rights reserved.
//

#import "TimeChartPoint.h"

#define kPointCount 542 //一天交易时间4个小时，240分钟

@implementation TimeChartPoint
- (instancetype)init
{
    self = [super init];
    [self initalization];
    return self;
}

- (void)initalization
{
    self.kLineWidth = 10.0f;
    self.intervalSpace = 1.0f;
}


- (NSMutableArray *)PointsTransFromModels:(NSArray *)models innerFrame:(CGRect)frame{
    NSMutableArray *pointArray = [NSMutableArray array];
    
    if (models.count == 0) return NULL;
    float yesClosePrice = 0;
    float maxPrice = 0;
    float minPrice = MAXFLOAT;
    float maxVolume = 0;
    if (yesClosePrice != 0) {
        minPrice = minPrice < yesClosePrice ? yesClosePrice : minPrice;  // 取小
    }
    
    float interValue = maxPrice - minPrice;
    
    if (interValue == 0) {  // maxPrice == minPrice
        interValue = maxPrice;
    }
    
    for (TimeModel *model in models) {
        maxPrice = MAX(maxPrice, model.Close);
        minPrice = MIN(minPrice, model.Close);
    }
    
    self.maxPrice = maxPrice;
    self.minPrice = minPrice;
    self.maxVolume = maxVolume;
    
    // 填充坐标系,因为line是添加到mainBox中，y轴起始点以0计算,否则要加上(+ frame.origin.y - frame.size.height)
    CGFloat height = frame.size.height;
    CGFloat width = frame.size.width-TIME_VIEW_RIGHT_SPACE;
    
    [models enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        CGFloat x = width / models.count * idx;
        
        float nowPri = [(TimeModel *)obj Close];
        
        TimeChartPoint *point = [[TimeChartPoint alloc] init];
        
        point.nowPriPoint = CGPointMake(x, (1-(nowPri - minPrice)/(maxPrice - minPrice))*height);
        
        [pointArray addObject:point];
        
    }];
    
    return pointArray;
}

@end
